package com.boolib.scheck.aop.common.exception;



import com.boolib.scheck.aop.common.ServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

@Slf4j
@RestControllerAdvice
public class GlobalUserAuthExceptionHandler  {



    @ExceptionHandler(UserAuthException.class)
    public ServerResponse<String> permissionsException(UserAuthException ex){
        return ServerResponse.createByErrorMessage(ex.getMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public ServerResponse<String> permissionsException(IllegalArgumentException ex){
        return ServerResponse.createByError(10,"参数异常错误");
    }


    @ExceptionHandler(BindException.class)
    public ServerResponse<String> bindException(BindException ex){
        List<ObjectError> errorList = ex.getAllErrors();
        return ServerResponse.createByError(10,errorList.get(0).getDefaultMessage());
    }

}
